/**
 * Created by chenxingfei on 2017/1/22.
 */

$(function () {
    alert($("#base").val());
    // alert($("#base").href);
    var base = document.getElementById("base").href;
    alert(base);
    alert('${ctx}');
    return ;
     $.ajax({
         async: false,
         url:"/blog-xingfei/code/imgCode",
         timeout: 5000,
         success: function(result) {
             var imgSrc = $("#imgObj");
             imgSrc.attr("src", result);
         },
         error: function(jqXHR, textStatus, errorThrown) {
             console.log("Get Upload URL Error!");
         }
     });
 })

// $(function () {
//     $.ajax({
//         async: false,
//         url:"/blog-xingfei/user/testBuildListJson",
//         timeout: 5000,
//         data:{'jsonData':getData()},
//         success: function(result) {
//             var imgSrc = $("#imgObj");
//             imgSrc.attr("src", result);
//         },
//         error: function(jqXHR, textStatus, errorThrown) {
//             console.log("Get Upload URL Error!");
//         }
//     });
// })

function getData() {
    var lData = [];
    var d1 = {};
    d1.id = "123";
    d1.phone = "13821012765";
    d1.email = "fhdskf@163.com";
    d1.password = "abc";
    d1.createTime = new Date();
    var d2 = {};
    d2.id = "123";
    d2.phone = "13821012765";
    d2.email = "fhdskf@163.com";
    d2.password = "abc";
    d2.createTime = new Date();
    var d3 = {};
    d3.id = "123";
    d3.phone = "13821012765";
    d3.email = "fhdskf@163.com";
    d3.password = "abc";
    d3.createTime = new Date();
    lData.push(d1);
    lData.push(d2);
    lData.push(d3);
    var jsonData = JSON.stringify(lData);
    return jsonData;
}
function changeImg() {
    var imgSrc = $("#imgObj");
    var src = imgSrc.attr("src");
    imgSrc.attr("src", chgUrl(src));
}
//时间戳
//为了使每次生成图片不一致，即不让浏览器读缓存，所以需要加上时间戳
function chgUrl(url) {
    var timestamp = (new Date()).valueOf();
    url = url.substring(0, 37);
    if ((url.indexOf("&") >= 0)) {
        url = url + "×tamp=" + timestamp;
    } else {
        url = url + "?timestamp=" + timestamp;
    }
    return url;
}


// 注册提交时的ajax请求.
var submitByAjax = function () {

    // 数据validate
    var email = $("#inputEmail").val();
    var phone = $("#inputPhone").val();
    var password = $("#inputPassword1").val();
    var passwordConfirm = $("#inputConfirmPassword1").val();
    var identifyingCode_img = $("#identifyingCode1").val();
    var identifyingCode_phone = $("#identifyingCode").val();
    var imgCodeLast = (identifyingCode_img==null || identifyingCode_img=='') ? identifyingCode_phone : identifyingCode_img;
    password = encryptByDES(password,"BLOG-XINGFEI-==")
    $.ajax({
        type: "post",
        url: "/jianyi-blog/user/register",
        data: {
            email: email,
            phone : phone,
            password: password,
            imgCode: imgCodeLast
        },
        dataType: "json",
        success: function (a) {
            if (a.errorCode == "200") {
                alert("注册成功！")
                // window.location.href = basePath + "reg/2.htm";
            } else {
                alert(a.errorMsg);
            }
        }
    });
    return false;
};

/**
 * 校验用户两次输入的密码是否一致
 * @param password
 * @param confirmPassword
 */
function checkPasswordConfirm(password, confirmPassword) {

}

/**
 * DES加密
 * @param message
 * @param key
 * @returns {*|string}
 */
function encryptByDES(message, key) {
    var keyHex = CryptoJS.enc.Utf8.parse(key);
    var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });
    return encrypted.toString();
}

///////////////////

var InterValObj; //timer变量，控制时间
var count = 60; //间隔函数，1秒执行
var curCount;//当前剩余秒数
// var codeLength = 6;//验证码长度
function sendMessage() {
    curCount = count;
    var phone=$("#inputPhone").val();//手机号码
    if(phone != ""){
        //产生验证码
        // for (var i = 0; i < codeLength; i++) {
        //     code += parseInt(Math.random() * 9).toString();
        // }
        //设置button效果，开始计时
        $("#messageCodeBtn").attr("disabled", "true");
        $("#messageCodeBtn").text(curCount + "秒后重新发送");
        InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
        //向后台发送处理数据
        $.ajax({
            type: "POST", //用POST方式传输
            dataType: "json", //数据格式:JSON
            url: '/jianyi-blog/code/sendMessageCode', //目标地址
            data: {'phone': phone},
            error: function (XMLHttpRequest, textStatus, errorThrown) { },
            success: function (msg){
                alert(msg.errorCode);
            }
        });
    }else{
        alert("手机号码不能为空！");
    }
}
//timer处理函数
function SetRemainTime() {
    if (curCount == 0) {
        window.clearInterval(InterValObj);//停止计时器
        $("#messageCodeBtn").removeAttr("disabled");//启用按钮
        $("#messageCodeBtn").text("重新发送验证码");
        code = ""; //清除验证码。如果不清除，过时间后，输入收到的验证码依然有效
    }
    else {
        curCount--;
        $("#messageCodeBtn").text(curCount + "秒后重新发送");
    }
}
